/*
 * DatagramBuilder Library: serialize/deserialize data to/from datagram
 * Copyright (C) 2008 Rakov Ilya <RakovIlya@gmail.com>
 *
 * This library can be distributed under the terms of the GNU LGPLv2.
 * See the file COPYING.LIB.
 */


#ifndef DATAGRAM_LISTBUILDER_H_
#define DATAGRAM_LISTBUILDER_H_


/**
 * @file ListBuilder.h
 *
 * @brief ListBuilder header file
 *
 * @date Dec 15, 2008
 * @author Rakov Ilya
 */


#include "Builder.h"
#include "Mark.h"
#include "Tag.h"


/**
 * @brief This template class provides an ability to serialize lists of the values.
 */
template< class ElemT, class CountT >
class ListBuilder
{

public:

  /**
   * @brief ctor.
   */
  ListBuilder( Builder &builder );


  /**
   * @return Count of the already serialized elements.
   */
  inline CountT GetCount() const;


public: /**@{ @name Serialization operators */

  /**
   * @brief Serializes element to the list
   *
   * @param value Data to serialize
   * @note Provides exception-safety.
   */
  inline ListBuilder& operator <<( ElemT &value );


  /**
   * @brief Serializes element to the list
   *
   * @param value Data to serialize
   * @note Provides exception-safety.
   */
  inline ListBuilder& operator <<( const ElemT &value );

/**@}*/


private: // implementation

  Builder      &builder_;
  Mark< CountT >  count_;

};


/*
 * @brief This template class provides an ability to serialize tagged lists of the values.
 */
template< class ElemT, class CountT >
class ListBuilder< Tag<ElemT>, CountT >
{

public:

  /**
   * @brief ctor.
   */
  ListBuilder( Builder &builder );


  /**
   * @return Count of the already serialized elements.
   */
  inline CountT GetCount() const;


public: /**@{ @name Serialization operators */

  /**
   * @brief Serializes element to the list
   *
   * @param value Data to serialize
   * @note Provides exception-safety.
   */
  inline ListBuilder& operator <<( ElemT &value );


  /**
   * @brief Serializes element to the list
   *
   * @param value Data to serialize
   * @note Provides exception-safety.
   */
  inline ListBuilder& operator <<( const ElemT &value );

/**@}*/


private:

  ListBuilder< ElemT, CountT > listBuilder_;

};


#include "impl/ListBuilderImpl.h"


#endif /* DATAGRAM_LISTBUILDER_H_ */
